package com.panshi.controller.user;


import com.panshi.api.*;
import com.panshi.bean.PageBean;
import com.panshi.po.AnswerPO;
import com.panshi.po.ArticlePo;
import com.panshi.po.QuestionPO;
import com.panshi.po.UserPO;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * 个人主页
 */
@Controller
@Slf4j
public class HomepageController {

       @Autowired
    private UserApi userApi;//用户

 /*   @Autowired
    private DynamicApi dynamicApi;//动态*/

    @Autowired
    private ArticleApi articleApi;//文章

    @Autowired
    private AnswerApi answerApi;//回答

    @Autowired
    private QuestionApi questionApi;//提问


    @GetMapping("homepage/{email}")
    public ModelAndView  information(@PathVariable String email,HttpServletRequest httpRequest){
        ModelAndView   modelAndView=new ModelAndView("user/userHomepage");

        UserPO userPO= userApi.findUserByEmail(email).getData();
        httpRequest.setAttribute("userPO",userPO);

        //提问
        Integer  integer=questionApi.findSumByUid(4).getData();
        log.info("提问==========={}",integer);
        modelAndView.addObject("integer",integer);



        //回答
        Integer in= answerApi.uidTotal(3).getData();
        log.info("回答in==========={}",in);
        modelAndView.addObject("in",in);


        //文章
        ArticleUidVo  articleUidVo=new ArticleUidVo();
        articleUidVo.setUid(3);
        Integer integer1=articleApi.uidQuery(articleUidVo);
        log.info("文章integer1=========={}",integer1);
        modelAndView.addObject("integer1",integer1);


            //提问
            FindByUidVO findByUidVO=new  FindByUidVO();
            findByUidVO.setUid(4);
            findByUidVO.setCurrentPage(1);
            findByUidVO.setCurrentPageSize(2);
            List<QuestionPO>  list=questionApi.findQuestionByUid(findByUidVO).getData();
            log.info("提问list========={}",list);
            modelAndView.addObject("findByUidVO",list);

            //回答
            AnswerUidQueryVO answerUidQueryVO=new AnswerUidQueryVO();
            answerUidQueryVO.setUid(3);
            answerUidQueryVO.setCurrenPage(1);
            answerUidQueryVO.setSize(2);
            List<AnswerPO>  answerPOS=answerApi.uidAnswer(answerUidQueryVO).getData();
            log.info("回答answerPOS=========={}",answerPOS);
            modelAndView.addObject("answerPOS",answerPOS);


            //文章
            ArticleUidQueryVo articleUidQueryVo=new ArticleUidQueryVo();
            articleUidQueryVo.setUid(3);
            articleUidQueryVo.setCurrentPage(1);
            articleUidQueryVo.setPageSize(2);

            List<ArticlePo> articlePos=articleApi.uidQueryContent( articleUidQueryVo).getData();

            log.info("文章articlePos========{}",articlePos);
            modelAndView.addObject("articlePos",articlePos);
        return modelAndView;
    }


    //个人主页
    @GetMapping("/uh/{num}/{email}")
    public ModelAndView uh(@PathVariable String num,@PathVariable String email, HttpServletRequest httpRequest){

        ModelAndView  modelAndView=new ModelAndView("user/userHomepage-list");

        UserPO userPO= userApi.findUserByEmail(email).getData();
        httpRequest.setAttribute("userPO",userPO);



        if("0".equals(num)){
            //提问
            FindByUidVO findByUidVO=new  FindByUidVO();
            findByUidVO.setUid(4);
            findByUidVO.setCurrentPage(1);
            findByUidVO.setCurrentPageSize(2);
            List<QuestionPO>  list=questionApi.findQuestionByUid(findByUidVO).getData();
            log.info("提问list========={}",list);
            modelAndView.addObject("findByUidVO",list);

            //回答
            AnswerUidQueryVO answerUidQueryVO=new AnswerUidQueryVO();
            answerUidQueryVO.setUid(3);
            answerUidQueryVO.setCurrenPage(1);
            answerUidQueryVO.setSize(2);
            List<AnswerPO>  answerPOS=answerApi.uidAnswer(answerUidQueryVO).getData();
            log.info("回答answerPOS========{}",answerPOS);
            modelAndView.addObject("answerPOS",answerPOS);

            //文章
            ArticleUidQueryVo articleUidQueryVo=new ArticleUidQueryVo();
            articleUidQueryVo.setUid(3);
            articleUidQueryVo.setCurrentPage(1);
            articleUidQueryVo.setPageSize(2);

            List<ArticlePo> articlePos=articleApi.uidQueryContent( articleUidQueryVo).getData();

            log.info("文章articlePos========{}",articlePos);
            modelAndView.addObject("articlePos",articlePos);

        }else if("1".equals(num)){
            FindByUidVO findByUidVO=new  FindByUidVO();
            findByUidVO.setUid(4);
            findByUidVO.setCurrentPage(1);
            findByUidVO.setCurrentPageSize(2);
            List<QuestionPO>  list=questionApi.findQuestionByUid(findByUidVO).getData();
            log.info("提问list========={}",list);
            modelAndView.addObject("findByUidVO",list);

        }else if("2".equals(num)){
            AnswerUidQueryVO answerUidQueryVO=new AnswerUidQueryVO();
            answerUidQueryVO.setUid(3);
            answerUidQueryVO.setCurrenPage(1);
            answerUidQueryVO.setSize(2);
            List<AnswerPO>  answerPOS=answerApi.uidAnswer(answerUidQueryVO).getData();

            IssueQueryVO issueQueryVO=new IssueQueryVO();
            issueQueryVO.setIid(8);
            Integer integer=answerApi.issueIdTotal(issueQueryVO).getData();
            httpRequest.setAttribute("integer",integer);
            log.info("回答总数integer--------------={}",integer);
            log.info("回答answerPOS=========={}",answerPOS);
            httpRequest.setAttribute("answerPOS",answerPOS);






        }else if("3".equals(num)){
            ArticleUidQueryVo articleUidQueryVo=new ArticleUidQueryVo();
            articleUidQueryVo.setUid(3);
            articleUidQueryVo.setCurrentPage(1);
            articleUidQueryVo.setPageSize(2);
            List<ArticlePo> articlePos=articleApi.uidQueryContent(articleUidQueryVo).getData();
            log.info("文章articlePos========{}",articlePos);
            modelAndView.addObject("articlePos",articlePos);
        }

        return modelAndView;
    }


    @PostMapping("/home")
    @ResponseBody
    public ModelAndView home(@RequestParam Integer current){

        ModelAndView  modelAndView=new ModelAndView("user/userHomepage-list");
        log.info("current===================={}",current);
        //提问
        FindByUidVO findByUidVO=new  FindByUidVO();
        findByUidVO.setUid(4);
        findByUidVO.setCurrentPage(current);
        findByUidVO.setCurrentPageSize(2);
        List<QuestionPO>  list=questionApi.findQuestionByUid(findByUidVO).getData();
        log.info("提问list========={}",list);
        modelAndView.addObject("findByUidVO",list);


        //回答
        AnswerUidQueryVO answerUidQueryVO=new AnswerUidQueryVO();
        answerUidQueryVO.setUid(3);
        answerUidQueryVO.setCurrenPage(current);
        answerUidQueryVO.setSize(2);
        List<AnswerPO>  answerPOS=answerApi.uidAnswer(answerUidQueryVO).getData();
        log.info("回答answerPOS=========={}",answerPOS);
        modelAndView.addObject("answerPOS",answerPOS);

        //文章
        ArticleUidQueryVo articleUidQueryVo=new ArticleUidQueryVo();
        articleUidQueryVo.setUid(3);
        articleUidQueryVo.setCurrentPage(current);
        articleUidQueryVo.setPageSize(2);

        List<ArticlePo> articlePos=articleApi.uidQueryContent( articleUidQueryVo).getData();

        log.info("文章articlePos========{}",articlePos);
        modelAndView.addObject("articlePos",articlePos);

        return modelAndView;
    }









}
